package org.apache.harmony.javax.security.auth;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.security.AccessControlContext;
import java.security.AccessController;
import java.security.DomainCombiner;
import java.security.Permission;
import java.security.Principal;
import java.security.PrivilegedAction;
import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;
import java.security.ProtectionDomain;
import java.util.AbstractSet;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Set;

public final class Subject
  implements Serializable
{
  private static final long serialVersionUID = -8308522755600156056L;
  private static final AuthPermission _AS = new AuthPermission("doAs");
  private static final AuthPermission _AS_PRIVILEGED = new AuthPermission("doAsPrivileged");
  private static final AuthPermission _SUBJECT = new AuthPermission("getSubject");
  private static final AuthPermission _PRINCIPALS = new AuthPermission("modifyPrincipals");
  private static final AuthPermission _PRIVATE_CREDENTIALS = new AuthPermission("modifyPrivateCredentials");
  private static final AuthPermission _PUBLIC_CREDENTIALS = new AuthPermission("modifyPublicCredentials");
  private static final AuthPermission _READ_ONLY = new AuthPermission("setReadOnly");
  private final Set<Principal> principals;
  private boolean readOnly;
  private transient SecureSet<Object> privateCredentials;
  private transient SecureSet<Object> publicCredentials;

  public Subject()
  {
    this.principals = new SecureSet(_PRINCIPALS);
    this.publicCredentials = new SecureSet(_PUBLIC_CREDENTIALS);
    this.privateCredentials = new SecureSet(_PRIVATE_CREDENTIALS);
    this.readOnly = false;
  }

  public Subject(boolean paramBoolean, Set<? extends Principal> paramSet, Set<?> paramSet1, Set<?> paramSet2)
  {
    if ((paramSet == null) || (paramSet1 == null) || (paramSet2 == null))
      throw new NullPointerException();
    this.principals = new SecureSet(_PRINCIPALS, paramSet);
    this.publicCredentials = new SecureSet(_PUBLIC_CREDENTIALS, paramSet1);
    this.privateCredentials = new SecureSet(_PRIVATE_CREDENTIALS, paramSet2);
    this.readOnly = paramBoolean;
  }

  public static Object doAs(Subject paramSubject, PrivilegedAction paramPrivilegedAction)
  {
    checkPermission(_AS);
    return doAs_PrivilegedAction(paramSubject, paramPrivilegedAction, AccessController.getContext());
  }

  public static Object doAsPrivileged(Subject paramSubject, PrivilegedAction paramPrivilegedAction, AccessControlContext paramAccessControlContext)
  {
    checkPermission(_AS_PRIVILEGED);
    if (paramAccessControlContext == null)
      return doAs_PrivilegedAction(paramSubject, paramPrivilegedAction, new AccessControlContext(new ProtectionDomain[0]));
    return doAs_PrivilegedAction(paramSubject, paramPrivilegedAction, paramAccessControlContext);
  }

  private static Object doAs_PrivilegedAction(Subject paramSubject, PrivilegedAction paramPrivilegedAction, AccessControlContext paramAccessControlContext)
  {
    final SubjectDomainCombiner localSubjectDomainCombiner;
    if (paramSubject == null)
      localSubjectDomainCombiner = null;
    else
      localSubjectDomainCombiner = new SubjectDomainCombiner(paramSubject);
    PrivilegedAction local1 = new PrivilegedAction()
    {
      public Object run()
      {
        return new AccessControlContext(Subject.this, localSubjectDomainCombiner);
      }
    };
    AccessControlContext localAccessControlContext = (AccessControlContext)AccessController.doPrivileged(local1);
    return AccessController.doPrivileged(paramPrivilegedAction, localAccessControlContext);
  }

  public static Object doAs(Subject paramSubject, PrivilegedExceptionAction paramPrivilegedExceptionAction)
    throws PrivilegedActionException
  {
    checkPermission(_AS);
    return doAs_PrivilegedExceptionAction(paramSubject, paramPrivilegedExceptionAction, AccessController.getContext());
  }

  public static Object doAsPrivileged(Subject paramSubject, PrivilegedExceptionAction paramPrivilegedExceptionAction, AccessControlContext paramAccessControlContext)
    throws PrivilegedActionException
  {
    checkPermission(_AS_PRIVILEGED);
    if (paramAccessControlContext == null)
      return doAs_PrivilegedExceptionAction(paramSubject, paramPrivilegedExceptionAction, new AccessControlContext(new ProtectionDomain[0]));
    return doAs_PrivilegedExceptionAction(paramSubject, paramPrivilegedExceptionAction, paramAccessControlContext);
  }

  private static Object doAs_PrivilegedExceptionAction(Subject paramSubject, PrivilegedExceptionAction paramPrivilegedExceptionAction, AccessControlContext paramAccessControlContext)
    throws PrivilegedActionException
  {
    final SubjectDomainCombiner localSubjectDomainCombiner;
    if (paramSubject == null)
      localSubjectDomainCombiner = null;
    else
      localSubjectDomainCombiner = new SubjectDomainCombiner(paramSubject);
    PrivilegedAction local2 = new PrivilegedAction()
    {
      public AccessControlContext run()
      {
        return new AccessControlContext(Subject.this, localSubjectDomainCombiner);
      }
    };
    AccessControlContext localAccessControlContext = (AccessControlContext)AccessController.doPrivileged(local2);
    return AccessController.doPrivileged(paramPrivilegedExceptionAction, localAccessControlContext);
  }

  public boolean equals(Object paramObject)
  {
    if (this == paramObject)
      return true;
    if ((paramObject == null) || (getClass() != paramObject.getClass()))
      return false;
    Subject localSubject = (Subject)paramObject;
    return (this.principals.equals(localSubject.principals)) && (this.publicCredentials.equals(localSubject.publicCredentials)) && (this.privateCredentials.equals(localSubject.privateCredentials));
  }

  public Set<Principal> getPrincipals()
  {
    return this.principals;
  }

  public <T extends Principal> Set<T> getPrincipals(Class<T> paramClass)
  {
    return ((SecureSet)this.principals).get(paramClass);
  }

  public Set<Object> getPrivateCredentials()
  {
    return this.privateCredentials;
  }

  public <T> Set<T> getPrivateCredentials(Class<T> paramClass)
  {
    return this.privateCredentials.get(paramClass);
  }

  public Set<Object> getPublicCredentials()
  {
    return this.publicCredentials;
  }

  public <T> Set<T> getPublicCredentials(Class<T> paramClass)
  {
    return this.publicCredentials.get(paramClass);
  }

  public int hashCode()
  {
    return this.principals.hashCode() + this.privateCredentials.hashCode() + this.publicCredentials.hashCode();
  }

  public void setReadOnly()
  {
    checkPermission(_READ_ONLY);
    this.readOnly = true;
  }

  public boolean isReadOnly()
  {
    return this.readOnly;
  }

  public String toString()
  {
    StringBuilder localStringBuilder = new StringBuilder("Subject:\n");
    Iterator localIterator = this.principals.iterator();
    while (localIterator.hasNext())
    {
      localStringBuilder.append("\tPrincipal: ");
      localStringBuilder.append(localIterator.next());
      localStringBuilder.append('\n');
    }
    localIterator = this.publicCredentials.iterator();
    while (localIterator.hasNext())
    {
      localStringBuilder.append("\tPublic Credential: ");
      localStringBuilder.append(localIterator.next());
      localStringBuilder.append('\n');
    }
    int i = localStringBuilder.length() - 1;
    localIterator = this.privateCredentials.iterator();
    try
    {
      while (localIterator.hasNext())
      {
        localStringBuilder.append("\tPrivate Credential: ");
        localStringBuilder.append(localIterator.next());
        localStringBuilder.append('\n');
      }
    }
    catch (SecurityException localSecurityException)
    {
      localStringBuilder.delete(i, localStringBuilder.length());
      localStringBuilder.append("\tPrivate Credentials: no accessible information\n");
    }
    return localStringBuilder.toString();
  }

  private void readObject(ObjectInputStream paramObjectInputStream)
    throws IOException, ClassNotFoundException
  {
    paramObjectInputStream.defaultReadObject();
    this.publicCredentials = new SecureSet(_PUBLIC_CREDENTIALS);
    this.privateCredentials = new SecureSet(_PRIVATE_CREDENTIALS);
  }

  private void writeObject(ObjectOutputStream paramObjectOutputStream)
    throws IOException
  {
    paramObjectOutputStream.defaultWriteObject();
  }

  public static Subject getSubject(AccessControlContext paramAccessControlContext)
  {
    checkPermission(_SUBJECT);
    if (paramAccessControlContext == null)
      throw new NullPointerException("auth.09");
    PrivilegedAction local3 = new PrivilegedAction()
    {
      public DomainCombiner run()
      {
        return Subject.this.getDomainCombiner();
      }
    };
    DomainCombiner localDomainCombiner = (DomainCombiner)AccessController.doPrivileged(local3);
    if ((localDomainCombiner == null) || (!(localDomainCombiner instanceof SubjectDomainCombiner)))
      return null;
    return ((SubjectDomainCombiner)localDomainCombiner).getSubject();
  }

  private static void checkPermission(Permission paramPermission)
  {
    SecurityManager localSecurityManager = System.getSecurityManager();
    if (localSecurityManager != null)
      localSecurityManager.checkPermission(paramPermission);
  }

  private void checkState()
  {
    if (this.readOnly)
      throw new IllegalStateException("auth.0A");
  }

  private final class SecureSet<SST> extends AbstractSet<SST>
    implements Serializable
  {
    private static final long serialVersionUID = 7911754171111800359L;
    private LinkedList<SST> elements;
    private int setType;
    private static final int SET_Principal = 0;
    private static final int SET_PrivCred = 1;
    private static final int SET_PubCred = 2;
    private transient AuthPermission permission;

    protected SecureSet(AuthPermission arg2)
    {
      Object localObject;
      this.permission = localObject;
      this.elements = new LinkedList();
    }

    protected SecureSet(Collection<? extends SST> arg2)
    {
      this(localAuthPermission);
      Object localObject1;
      int i = localObject1.getClass().getClassLoader() == null ? 1 : 0;
      Iterator localIterator = localObject1.iterator();
      while (localIterator.hasNext())
      {
        Object localObject2 = localIterator.next();
        verifyElement(localObject2);
        if ((i != 0) || (!this.elements.contains(localObject2)))
          this.elements.add(localObject2);
      }
    }

    private void verifyElement(Object paramObject)
    {
      if (paramObject == null)
        throw new NullPointerException();
      if ((this.permission == Subject._PRINCIPALS) && (!Principal.class.isAssignableFrom(paramObject.getClass())))
        throw new IllegalArgumentException("auth.0B");
    }

    public boolean add(SST paramSST)
    {
      verifyElement(paramSST);
      Subject.this.checkState();
      Subject.checkPermission(this.permission);
      if (!this.elements.contains(paramSST))
      {
        this.elements.add(paramSST);
        return true;
      }
      return false;
    }

    public Iterator<SST> iterator()
    {
      // Byte code:
      //   0: aload_0
      //   1: getfield 28	org/apache/harmony/javax/security/auth/Subject$SecureSet:permission	Lorg/apache/harmony/javax/security/auth/AuthPermission;
      //   4: invokestatic 51	org/apache/harmony/javax/security/auth/Subject:access$3	()Lorg/apache/harmony/javax/security/auth/AuthPermission;
      //   7: if_acmpne +19 -> 26
      //   10: new 22	org/apache/harmony/javax/security/auth/Subject$SecureSet$1
      //   13: dup
      //   14: aload_0
      //   15: aload_0
      //   16: getfield 27	org/apache/harmony/javax/security/auth/Subject$SecureSet:elements	Ljava/util/LinkedList;
      //   19: invokevirtual 46	java/util/LinkedList:iterator	()Ljava/util/Iterator;
      //   22: invokespecial 56	org/apache/harmony/javax/security/auth/Subject$SecureSet$1:<init>	(Lorg/apache/harmony/javax/security/auth/Subject$SecureSet;Ljava/util/Iterator;)V
      //   25: areturn
      //   26: new 24	org/apache/harmony/javax/security/auth/Subject$SecureSet$SecureIterator
      //   29: dup
      //   30: aload_0
      //   31: aload_0
      //   32: getfield 27	org/apache/harmony/javax/security/auth/Subject$SecureSet:elements	Ljava/util/LinkedList;
      //   35: invokevirtual 46	java/util/LinkedList:iterator	()Ljava/util/Iterator;
      //   38: invokespecial 58	org/apache/harmony/javax/security/auth/Subject$SecureSet$SecureIterator:<init>	(Lorg/apache/harmony/javax/security/auth/Subject$SecureSet;Ljava/util/Iterator;)V
      //   41: areturn
    }

    public boolean retainAll(Collection<?> paramCollection)
    {
      if (paramCollection == null)
        throw new NullPointerException();
      return super.retainAll(paramCollection);
    }

    public int size()
    {
      return this.elements.size();
    }

    protected final <E> Set<E> get(final Class<E> paramClass)
    {
      if (paramClass == null)
        throw new NullPointerException();
      AbstractSet local2 = new AbstractSet()
      {
        private LinkedList<E> elements = new LinkedList();

        public boolean add(E paramAnonymousE)
        {
          if (!paramClass.isAssignableFrom(paramAnonymousE.getClass()))
            throw new IllegalArgumentException("auth.0C " + paramClass.getName());
          if (this.elements.contains(paramAnonymousE))
            return false;
          this.elements.add(paramAnonymousE);
          return true;
        }

        public Iterator<E> iterator()
        {
          return this.elements.iterator();
        }

        public boolean retainAll(Collection<?> paramAnonymousCollection)
        {
          if (paramAnonymousCollection == null)
            throw new NullPointerException();
          return super.retainAll(paramAnonymousCollection);
        }

        public int size()
        {
          return this.elements.size();
        }
      };
      Iterator localIterator = iterator();
      while (localIterator.hasNext())
      {
        Object localObject = localIterator.next();
        if (paramClass.isAssignableFrom(localObject.getClass()))
          local2.add(paramClass.cast(localObject));
      }
      return local2;
    }

    private void readObject(ObjectInputStream paramObjectInputStream)
      throws IOException, ClassNotFoundException
    {
      paramObjectInputStream.defaultReadObject();
      switch (this.setType)
      {
      case 0:
        this.permission = Subject._PRINCIPALS;
        break;
      case 1:
        this.permission = Subject._PRIVATE_CREDENTIALS;
        break;
      case 2:
        this.permission = Subject._PUBLIC_CREDENTIALS;
        break;
      default:
        throw new IllegalArgumentException();
      }
      Iterator localIterator = this.elements.iterator();
      while (localIterator.hasNext())
        verifyElement(localIterator.next());
    }

    private void writeObject(ObjectOutputStream paramObjectOutputStream)
      throws IOException
    {
      if (this.permission == Subject._PRIVATE_CREDENTIALS)
      {
        Iterator localIterator = iterator();
        while (localIterator.hasNext())
          localIterator.next();
        this.setType = 1;
      }
      else if (this.permission == Subject._PRINCIPALS)
      {
        this.setType = 0;
      }
      else
      {
        this.setType = 2;
      }
      paramObjectOutputStream.defaultWriteObject();
    }

    private class SecureIterator
      implements Iterator<SST>
    {
      protected Iterator<SST> iterator;

      protected SecureIterator()
      {
        Object localObject;
        this.iterator = localObject;
      }

      public boolean hasNext()
      {
        return this.iterator.hasNext();
      }

      public SST next()
      {
        return this.iterator.next();
      }

      public void remove()
      {
        Subject.this.checkState();
        Subject.checkPermission(Subject.SecureSet.this.permission);
        this.iterator.remove();
      }
    }
  }
}

/* Location:           D:\hygieia\userclient\common\libs\easemobchat_2.1.8.jar
 * Qualified Name:     org.apache.harmony.javax.security.auth.Subject
 * JD-Core Version:    0.6.1
 */